package com.base.myproject.client.HR.Training;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.core.client.GWT;
/**
 * 培训类型定义
 * @author Administrator
 *
 */
public class Training_TypeSetting extends BaseJDBCPanel{

	BaseComboBoxX checktype = new BaseComboBoxX("name","dictionary","where type='hr_trainingtype'");
	BaseComboBoxForm<BaseModelData> sectionCode = new BaseComboBoxForm<BaseModelData>("id","name","SystemInfo","where len(id)=(select max(len(id)) from systeminfo)",true);
	BaseComboBoxX position = new BaseComboBoxX("name","dictionary","where type='hr_title'");
	BaseComboBoxX keydate = new BaseComboBoxX("name","dictionary","where type='hr_keydate'");
	ColumnConfig C6 = null; //分部门
	ColumnConfig C3 = null; //职务
	PagingToolBar toolBar1 = null;
	PagingToolBar toolBar2 = null;
	
	public Training_TypeSetting(){
		init();
	}
	
	public void complete(){
		super.complete();
	}
	
	public void init(){
		initWidget();
		registerMainStore(store);
		registerStore(store);
		position.addListener(Events.BeforeQuery,getListener());
	}
	
	public void initWidget(){
		toolBar1 = new PagingToolBar(12);
		toolBar2 = new PagingToolBar(12);
		HorizontalPanel cp = new HorizontalPanel();	
		cp.setSpacing(2);
		
		ContentPanel panel = new ContentPanel();
		panel.setHeaderVisible(false);
		panel.add(this.getGrid());
		panel.setBottomComponent(toolBar1);
		
		ContentPanel panel2 = new ContentPanel();
		panel2.setHeaderVisible(false);
		panel2.add(this.getGrid2());
		panel2.setBottomComponent(toolBar2);
		
		cp.add(panel);
		cp.add(panel2);
		cp.add(this.getGrid3());
		cp.setStyleName("hp_css");//添加一个css属性
		this.grid.addListener(Events.OnClick, getListener());
		add(cp);
	}
	
	BaseStore<BusiObject> store = null;
	public BaseStore<BusiObject> getStore(){
		if(store == null){
			createStore();
		}
		store.setToolBar(toolBar1);
		store.load();
		return store;
	}
	
	ModelType type = new ModelType();
	private BaseStore<BusiObject> createStore(){
		
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		
		type.addField("typeCode");
		type.getField("typeCode").setType(String.class);
		
		type.addField("typeName");
		type.getField("typeName").setType(String.class);
		
		type.addField("checkType");
		type.getField("checkType").setType(String.class);
		
		type.addField("topScore");
		type.getField("topScore").setType(Integer.class);
		
		type.addField("keyDate");
		type.getField("keyDate").setType(String.class);
		
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_TrainingTypeDefine");
		info.setKeyname(new String[] { "icode"});
		info.setSql("select * from hr_TrainingTypeDefine");
		store = new BaseStore<BusiObject>(type, info);
		return null;
	}
	
	BaseGrid grid = null;
	public BaseGrid getGrid() {
		if (grid == null) {
			createGrid();
		}
		return grid;
	}
	
	private void createGrid(){
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		DateField dateField1 = new DateField();
		
		ColumnConfig C5 = new ColumnConfig("typeCode", "编号", 60);
		columns.add(C5);
		
		ColumnConfig C6 = new ColumnConfig("typeName", "培训类型名称", 120);
		C6.setEditor(new CellEditor(new TextField<String>()));
		columns.add(C6);
		
		ColumnConfig C3 = new ColumnConfig("checkType", "考核指标", 80);
		checktype.setName("checkType");
		C3.setEditor(checktype.getCellEditor());
		GridCellRenderer<ModelData>	renderer = new GridCellRenderer<ModelData>() {              
		      public String render(ModelData model, String property, ColumnData d, int rowIndex,
		          int colIndex, ListStore<ModelData> store, Grid<ModelData> grid) {             
		    	  if(model.get(property) !=null && model.get(property) instanceof ModelData)
		    	  {
		    		  return ((ModelData)model.get(property)).get("value");
		    	  }                                                                            
		    	  return model.get(property);                                                     
		      }                                                                                 
		    };  
		C3.setRenderer(renderer);
		columns.add(C3);
		
		ColumnConfig C7 = new ColumnConfig("topScore", "满分", 30);
		C7.setEditor(new CellEditor(new TextField<String>()));
		columns.add(C7);
		
		ColumnConfig C8 = new ColumnConfig("keyDate", "统计日期", 60);
		keydate.setName("keyDate");
		C8.setEditor(keydate.getCellEditor());
		columns.add(C8);
		C8.setRenderer(renderer);
		
		
		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid(getStore(), cm){
			@SuppressWarnings("unchecked")
			public void addnewrecord() {
				int rows = store.getCount() + 1;
				String typecode = "TTD" + 0 + rows;
				HashMap hp = new HashMap();
				hp.put("typeCode", typecode);
				grid.getStore().add(hp);
			}
		};
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setWidth(400);
		grid.setHeight(400);
		grid.setLoadMask(true);
		grid.setBorders(false);
		//Tools.setResize(grid,0.33,0.95);
	}
	
	BaseStore<BusiObject> store2 = null;
	public BaseStore<BusiObject> getStore2(){
		if(store2 == null){
			createStore2();
		}
		store2.setToolBar(toolBar2);
		//store2.initcompleted();
		return store2;
	}
	
	
	private BaseStore<BusiObject> createStore2(){
		
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		
		type.addField("typeCode");
		type.getField("typeCode").setType(String.class);
		
		type.addField("sectionCode");
		type.getField("sectionCode").setType(String.class);
		
		type.addField("insidePosition");
		type.getField("insidePosition").setType(String.class);
		
		type.addField("content");
		type.getField("content").setType(String.class);
		
		type.addField("grade");
		type.getField("grade").setType(String.class);
		
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_TrainingType_content");
		info.setKeyname(new String[] { "icode"});
		info.setSql("select * from hr_TrainingType_content where typeCode = '{typeCode}'");
		info.setParent(getStore());
		info.setLinkparentcolumns(new String[] { "typeCode" }); 
		info.setLinkparent_chile_columns(new String[] { "typeCode"  });
		store2 = new BaseStore<BusiObject>(type, info);
		return null;
	}
	
	BaseGrid grid2 = null;
	public BaseGrid getGrid2() {
		if (grid2 == null) {
			createGrid2();
		}
		return grid2;
	}
	
	private void createGrid2(){
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		DateField dateField1 = new DateField();
		
		C6 = new ColumnConfig("sectionCode", "分部门名称", 100);
		C6.setEditor(sectionCode.getCelEditor());
		C6.setRenderer(sectionCode);
		C6.setHidden(true);
		columns.add(C6);
		
		C3 = new ColumnConfig("insidePosition", "职务", 120);
		//C3.setEditor(new CellEditor(position));
		C3.setEditor(position.getCellEditor());
		GridCellRenderer<ModelData>	renderer = new GridCellRenderer<ModelData>() {              
		      public String render(ModelData model, String property, ColumnData d, int rowIndex,
		          int colIndex, ListStore<ModelData> store, Grid<ModelData> grid) {             
		    	  if(model.get(property) !=null && model.get(property) instanceof ModelData)
		    	  {
		    		  return ((ModelData)model.get(property)).get("value");
		    	  }                                                                            
		    	  return model.get(property);                                                     
		      }                                                                                 
		    };  
		C3.setRenderer(renderer);
		C3.setHidden(true);
		columns.add(C3);
		
		ColumnConfig C4 = new ColumnConfig("content", "内容", 250);
		TextField<String> content = new TextField<String>();
		content.setName("content");
		C4.setEditor(new CellEditor(content));
		C4.setRenderer(new GridCellRenderer<ModelData>() {              
		       public String render(ModelData model, String property, ColumnData d, int rowIndex,
		          int colIndex, ListStore<ModelData> store, Grid<ModelData> grid) {             
		    	  StringBuffer content = new StringBuffer();
		    	  content.append("<div style=\"white-space:normal; \" >");
		    	  content.append(model.<String>get("content"));
		    	  content.append("</div>");
	              return content.toString();                                                   
		      }                                                                                 
		    });
		columns.add(C4);
		
		ColumnConfig C2 = new ColumnConfig("grade", "评定（1分，0.5分）", 120);
		C2.setEditor(new CellEditor(new TextField<String>()));
		C2.setHidden(true);
		columns.add(C2);
		
		ColumnModel cm = new ColumnModel(columns);
		grid2 = new BaseGrid(getStore2(), cm,true,true);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid2.setSelectionModel(cbs);
		grid2.setWidth(332);
		grid2.setHeight(400);
		grid2.setLoadMask(true);
		grid2.setBorders(false);
		//Tools.setResize(grid2,0.42,0.95);
	}
	
	BaseStore<BusiObject> store3 = null;
	public BaseStore<BusiObject> getStore3(){
		if(store3 == null){
			createStore3();
		}
		return store3;
	}
	
	
	private BaseStore<BusiObject> createStore3(){
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		
		type.addField("TrainingType_content_icode");
		type.getField("TrainingType_content_icode").setType(Long.class);
		
		type.addField("contentExplain");
		type.getField("contentExplain").setType(String.class);
		
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_TrainingType_content_list");
		info.setKeyname(new String[] { "icode"});
		info.setSql("select * from hr_TrainingType_content_list where TrainingType_content_icode = '{icode}'");
		info.setParent(getStore2());
		info.setLinkparentcolumns(new String[] { "icode" }); 
		info.setLinkparent_chile_columns(new String[] { "TrainingType_content_icode" });
		store3 = new BaseStore<BusiObject>(type, info);
		return null;
	}
	
	BaseGrid grid3 = null;
	public BaseGrid getGrid3() {
		if (grid3 == null) {
			createGrid3();
		}
		return grid3;
	}
	
	private void createGrid3(){
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		DateField dateField1 = new DateField();
		
		ColumnConfig C6 = new ColumnConfig("contentExplain", "内容分解", 260);
		C6.setEditor(new CellEditor(new TextField<String>()));
		columns.add(C6);
		
		ColumnModel cm = new ColumnModel(columns);
		grid3 = new BaseGrid(getStore3(), cm,true,true);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid3.setSelectionModel(cbs);
		grid3.setWidth(320);
		grid3.setHeight(427);
		//Tools.setResize(grid3,0.28,0.95);
	}
	
	int sync = 0;                                                      
	Listener<BaseEvent> defaultlistener;                               
                                                                     
	public Listener<BaseEvent> getListener() {                                
		if(defaultlistener!=null)                                        
		{                                                                
			return defaultlistener;                                        
		}                                                                
                                                                     
		defaultlistener = new Listener<BaseEvent>() {                    
			public void handleEvent(BaseEvent be) {                        
				sync++;                                                    
				try {                                                      
					if (sync == 1) {                                   
						if (be.getType() == Events.Select) { 
							
						}else if(be.getType() == Events.OnBlur){
							
						}else if(be.getType() == Events.OnClick){
							if(grid.getSelectionModel().getSelectedItem() != null){
								String value = grid.getSelectionModel().getSelectedItem().get("typeCode");
								if(value.equals("TTD003") || value.equals("TTD004")){
									grid2.getColumnModel().setHidden(2,false);
									grid2.getColumnModel().setHidden(3,false);
								}else{
									grid2.getColumnModel().setHidden(2,true);
									grid2.getColumnModel().setHidden(3,true);
								}
							}
						}else if(be.getType() == Events.BeforeQuery){
							if(be.getSource().equals(position)){
								if(getStore2().getCount() != 0){
									try{
										String department = grid2.getSelectionModel().getSelectedItem().get("sectionCode");
									//	String dep = department.get("code");
										position.setFilter("WHERE department like '"+department+"%25'");
									}catch(Exception e){
										e.printStackTrace();
									}
								}else{
									MessageBox.alert("提示", "请选择您要修改的数据！", null);
								}
							}
						}
					}                                                  
				} finally {                                                
					sync--;                                            
				}                                                          
			}                                                              
		};                                                               
		return defaultlistener;                                          
	}
}
